package hr.fer.pus.dll_will.sp.data;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * Data extraction from XML.
 */
public class XMLMiner {
	
	/** Private construtor to prevent creation of objects. */
	private XMLMiner() {};
	
	/**
	 * Get all users from XML.
	 * 
	 * @param xmlFilePath Path to XML file containing users.
	 * @return Collection of users from given XML.
	 * @throws IOException
	 */
	public static Set<User> getUsers(String xmlFilePath) throws IOException {
		return getUsers(new File(xmlFilePath));
	}
	
	/**
	 * Get all users from XML.
	 * 
	 * @param xmlFile XML file containing users.
	 * @return Collection of users from given XML.
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Set<User> getUsers(File xmlFile) throws IOException {
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
		
		try {
			document = builder.build(xmlFile);
		} catch (JDOMException e) {
			throw new IOException(e);
		}
		
		Element root = document.getRootElement();
		List<Element> children = (List<Element>) root.getChildren();
		Set<User> users = new HashSet<User>();
	    
		for (Element doc : children){
			User u = new User();
			u.setId(Long.parseLong(getChild(doc, "id").getText()));
			u.setPassword(getChild(doc, "password").getText());
			u.setUsername(getChild(doc, "username").getText());
			
			users.add(u);
		}
		
		return users;
	}
	
	/**
	 * Get all files from XML.
	 * 
	 * @param xmlFilePath Path to XML file containing files.
	 * @return Collection of files from given XML.
	 * @throws IOException
	 */
	public static Set<ServiceFile> getFiles(String xmlFilePath) throws IOException {
		return getFiles(new File(xmlFilePath));
	}
	
	/**
	 * Get all files from XML.
	 * 
	 * @param xmlFile XML file containing files.
	 * @return Collection of files from given XML.
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Set<ServiceFile> getFiles(File xmlFile) throws IOException {
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
		
		try {
			document = builder.build(xmlFile);
		} catch (JDOMException e) {
			throw new IOException(e);
		}
		
		Element root = document.getRootElement();
		List<Element> children = (List<Element>) root.getChildren();
		Set<ServiceFile> files = new HashSet<ServiceFile>();
	    
		for (Element doc : children){
			ServiceFile file = new ServiceFile();
			file.setId(Long.parseLong(getChild(doc, "id").getText()));
			file.setOwnerId(Long.parseLong(getChild(doc, "ownerId").getText()));
			file.setContent(getChild(doc, "content").getText());
			file.setFilename(getChild(doc, "filename").getText());
			file.setDescription(getChild(doc, "description").getText());
			
			files.add(file);
		}
		
		return files;
	}
	
	@SuppressWarnings("unchecked")
	private static Element getChild(Element elem, String name){
		
		List<Element> children = (List<Element>) elem.getChildren();
		
		for (Element c : children){
	    	  if (c.getName().equals(name)){
	    		  return c;
	    	  }
	    }
		
		return null; //no such element
	}
	
}
